using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using MyFrame.Pool;
using Plugin.Tool;
using UnityEngine;

public class BloodParticleController : MonoBehaviour
{
    private const string stainPrefabName = "Bloodstain"; // 痕迹预制体
    private const string splatPrefabName = "BloodSplat";// 飞溅预制体
    ParticleSystem particle;  // 粒子系统对象
    private List<ParticleCollisionEvent> collisionEvents = new List<ParticleCollisionEvent>();  // 碰撞事件列表

    private void Start()
    {
        particle = GetComponent<ParticleSystem>();  // 获取粒子系统组件
    }

    private void OnParticleCollision(GameObject other)
    {
        ParticlePhysicsExtensions.GetCollisionEvents(particle, other, collisionEvents);  // 获取碰撞事件

        int count = collisionEvents.Count;  // 碰撞事件数量
        for (int i = 0; i < count; i++)
        {
            //地面处理
            if (collisionEvents[i].colliderComponent.gameObject.layer == LayerMask.NameToLayer("Ground"))
            {
                // 在碰撞点实例化痕迹预制体
                GameObject go = PoolMgr.Instance.GetObj(stainPrefabName, collisionEvents[i].intersection, Quaternion.Euler(0, 0, Random.Range(0.0f, 360.0f)));
                go.transform.localPosition = collisionEvents[i].intersection;
            }
            //背景处理  需要3d碰撞 参考盐和避难所
            // if (collisionEvents[i].colliderComponent.gameObject.layer == LayerMask.NameToLayer("Background"))
            // {
            //     // 在碰撞点实例化飞溅预制体
            //     GameObject bloodSplat = PoolMgr.Instance.GetObj(splatPrefabName, collisionEvents[i].intersection, Quaternion.Euler(0, 0, Random.Range(0.0f, 360.0f)));
            //     // 随机化粒子大小
            //     float randomScale = UnityEngine.Random.Range(1f, 3f);
            //     bloodSplat.transform.localScale = new Vector3(randomScale, randomScale, 1);
            //     ZTimer.SetTimeout(3f, () => PoolMgr.Instance.Release(bloodSplat));
            // }
        }
    }
}
